package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.RecordEngin;
import com.ruoyi.system.domain.vo.BarChartVo;
import com.ruoyi.system.mapper.RecordEnginMapper;
import com.ruoyi.system.service.IRecordEnginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 项目工程管理Service业务层处理
 *
 * @author admin
 * @date 2022-03-02
 */
@Service
public class RecordEnginServiceImpl implements IRecordEnginService {
    private static final Logger log = LoggerFactory.getLogger(RecordEnginServiceImpl.class);
    @Autowired
    protected Validator validator;
    @Autowired
    private RecordEnginMapper recordEnginMapper;

    /**
     * 查询项目工程管理
     *
     * @param enginId 项目工程管理主键
     * @return 项目工程管理
     */
    @Override
    public RecordEngin selectRecordEnginByEnginId(Long enginId) {
        return recordEnginMapper.selectRecordEnginByEnginId(enginId);
    }

    /**
     * 查询项目工程管理列表
     *
     * @param recordEngin 项目工程管理
     * @return 项目工程管理
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<RecordEngin> selectRecordEnginList(RecordEngin recordEngin) {
        return recordEnginMapper.selectRecordEnginList(recordEngin);
    }

    // 按单位工程名称查询
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<RecordEngin> selectRecordEnginListByName(RecordEngin recordEngin) {
        return recordEnginMapper.selectRecordEnginListByName(recordEngin);
    }

    /**
     * 新增项目工程管理
     *
     * @param recordEngin 项目工程管理
     * @return 结果
     */
    @Override
    public int insertRecordEngin(RecordEngin recordEngin) {
        recordEngin.setObjInt11(SecurityUtils.getUserId());// 创建人ID，用于数据权限
        recordEngin.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        recordEngin.setCreateTime(DateUtils.getNowDate());
        return recordEnginMapper.insertRecordEngin(recordEngin);
    }

    /**
     * 修改项目工程管理
     *
     * @param recordEngin 项目工程管理
     * @return 结果
     */
    @Override
    public int updateRecordEngin(RecordEngin recordEngin) {
        recordEngin.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        recordEngin.setUpdateTime(DateUtils.getNowDate());
        return recordEnginMapper.updateRecordEngin(recordEngin);
    }

    /**
     * 批量删除项目工程管理
     *
     * @param enginIds 需要删除的项目工程管理主键
     * @return 结果
     */
    @Override
    public int deleteRecordEnginByEnginIds(Long[] enginIds) {
        StringBuilder failureMsg = new StringBuilder();
        try {
            return recordEnginMapper.deleteRecordEnginByEnginIds(enginIds);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("项目已挂案卷，禁止删除！");
        }
    }

    /**
     * 删除项目工程管理信息
     *
     * @param enginId 项目工程管理主键
     * @return 结果
     */
    @Override
    public int deleteRecordEnginByEnginId(Long enginId) {
        return recordEnginMapper.deleteRecordEnginByEnginId(enginId);
    }

    @Override
    public String importEngin(List<RecordEngin> enginList, String operName) {
        if (StringUtils.isNull(enginList) || enginList.size() == 0) {
            throw new ServiceException("导入项目工程数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RecordEngin engin : enginList) {
            try {
                BeanValidators.validateWithException(validator, engin);
                engin.setCreateBy(operName);
                this.insertRecordEngin(engin);
                successNum++;
                successMsg.append("<br/>" + successNum + "、项目工程 " + engin.getEnginName() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、项目工程 " + engin.getEnginName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确或项目工程名称重复，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    // 状态图、饼状图数据查询
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BarChartVo> selectBarChartVoList(BarChartVo barChartVo) {
        return recordEnginMapper.selectBarChartVoList(barChartVo);
    }

}
